package com.jiaai.web.xiaoke.family;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.exception.ServerException;
import com.jiaai.family.api.TFamilyApi;
import com.jiaai.family.model.TFamilyVO;
import com.jiaai.family.model.TmemberVO;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.system.api.ISystemConfigApi;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title FamilyController
 * @since 2017/12/27
 */
@RestController
@RequestMapping("/api/v1/app/family")
public class AppFamilyController {
    @Reference
    private TFamilyApi tFamilyApi;

    @Reference
    private ISystemConfigApi iSystemConfigApi;

    /**
     * 获取所有家族列表
     * @param curr
     * @param tFamilyVO
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAll")
    @ResponseBody
    public Object findByAll(@RequestParam(defaultValue = "0", required = false) Integer curr, TFamilyVO tFamilyVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tFamilyApi.findByAll(curr, tFamilyVO));
    }

    /**
     * 获取所有的会员
     * @param curr
     * @param tmemberVO
     * @return
     * @throws Exception
     */
    @GetMapping("/allmenmber")
    @ResponseBody
    public Object allmenmber(@RequestParam(defaultValue = "0", required = false) Integer curr, TmemberVO tmemberVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tFamilyApi.allmenmber(curr, tmemberVO));
    }

    /**
     * 获取单个家族
     * @param fyId
     * @return
     * @throws Exception
     */
    @GetMapping("/findById")
    public Object findById(@RequestParam Integer fyId) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tFamilyApi.findById(fyId));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }
    /**
     * 获取单个会员的所有的家族
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/myFamily")
    @TokenIntercept(checkVisitor = true)
    public Object all() throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
            return RestResultGenerator.genSuccessResult(tFamilyApi.all(accessUser.getUserId()));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }
    /**
     * 统计单个家族所有成员
     * @param fyId
     * @return
     * @throws Exception
     */
    @GetMapping("/count")
    public Object count(Integer fyId) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tFamilyApi.count(fyId));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }
    /**
     * 删除单个家族
     * @param fyId
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @TokenIntercept(checkVisitor = true)
    public Object delete(Integer fyId) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
            if(((TFamilyVO)tFamilyApi.findById(fyId)).getmId().equals(accessUser.getUserId())){
                tFamilyApi.delete(fyId);
                return RestResultGenerator.genSuccessResult("删除成功");
            } else {
                throw new ServerException("暂无权限");
            }
        } catch (Exception e) {
            throw new ServerException("删除失败");
        }
    }
    /**
     * 添加家族
     * @param tFamilyVO
     * @return
     * @throws Exception
     */
    @PostMapping("/addFamily")
    @TokenIntercept(checkVisitor = true)
    public Object addFamily( TFamilyVO tFamilyVO ) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
            tFamilyVO.setmId(accessUser.getUserId());
            tFamilyApi.addFamily(tFamilyVO);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }

    /**
     * 修改家族
     * @param  tFamilyVO
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @TokenIntercept(checkVisitor = true)
    public Object update(@RequestBody TFamilyVO tFamilyVO) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TFamilyVO)tFamilyApi.findById(tFamilyVO.getFyId())).getmId().equals(accessUser.getUserId())
          if(true)  {
                tFamilyApi.update(tFamilyVO);
                return RestResultGenerator.genSuccessResult("修改成功");
            } else {
              return "暂无权限修改";
          }
        } catch (Exception e) {
            throw new ServerException("修改失败");
        }
    }

    /**
     * 获取单个家族下面的所有成员
     * @param curr
     * @param tFamilyVO
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAllRole")
    @ResponseBody
    public Object findByAllRole(@RequestParam(defaultValue = "1", required = false) Integer curr, TFamilyVO tFamilyVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tFamilyApi.findByAllRole(curr, tFamilyVO));
    }

}
